
clear all
use "./longbase3.dta"
set type double
xtset cnum time

* ayear: JFY (Japanese Fiscal Year)
gen amonth = L3.month
replace amonth = 10 if year == 1996 & month == 1
replace amonth = 11 if year == 1996 & month == 2
replace amonth = 12 if year == 1996 & month == 3
gen ayear = L3.year
replace ayear = 1995 if year == 1996 & month == 1
replace ayear = 1995 if year == 1996 & month == 2
replace ayear = 1995 if year == 1996 & month == 3

********************** GET exchange rate ***********************
merge 1:1 cnum time using jpylcc
drop if _merge ==2
drop _merge
rename c jpylcc

********************** GET DATA **********************************
local chicken /// 
20711000 20712000 20713100 20713200 20714100 20714210 20714220

local meat ///
20711000 20712000 20713100 20713200 20714100 20714210 20714220 ///
160231210 160232210 160232290 160239210 

local others 160231210 160232210 160232290 160239210

* Obtain kjpy kgrm (Note: All3 contains monthly cumulative data)
foreach s of local meat{
gen hscode = `s'
merge 1:1 cnum time hscode using All3, keepusing(kg kjpy)
drop if _merge == 2
gen ckjpy`s' = kjpy
gen ckgrm`s' = kg
drop kjpy kg hscode _merge
      replace ckgrm`s' = 0 if missing(ckgrm`s')
	  replace ckjpy`s' = 0 if missing(ckjpy`s')
* Generate the frame
gen kgrm`s' =0
gen kjpy`s' =0
* Un-cumulate ckgrm ckjpy
sort cnum time
replace kgrm`s' = ckgrm`s' - L.ckgrm`s'
replace kgrm`s' = ckgrm`s' if month == 1
replace kjpy`s' = ckjpy`s' - L.ckjpy`s'
replace kjpy`s' = ckjpy`s' if month == 1
}
drop ckgrm* ckjpy*

* General tariff rates (all years)
gen tr20711000 = 14 //68
gen tr20712000 = 14 //69
gen tr20713100 = 20 //70
gen tr20713200 = 12 //71
gen tr20714100 = 10 //72
gen tr20714210 = 20 //73
gen tr20714220 = 12 //74

* Applied MFN = WTO (2000- )
replace tr20711000 = 11.9 if wto == "WTO"
replace tr20712000 = 11.9 if wto == "WTO"
replace tr20713100 = 8.5  if wto == "WTO"
replace tr20713200 = 11.9 if wto == "WTO"
replace tr20714100 = 3    if wto == "WTO"
replace tr20714210 = 8.5  if wto == "WTO"
replace tr20714220 = 11.9 if wto == "WTO"
* Applied MFN = WTO (96march-96April)
replace tr20711000 = 13.7 if year == 1996 & wto == "WTO"
replace tr20712000 = 13.7 if year == 1996 & wto == "WTO"
replace tr20713100 = 9.8  if year == 1996 & wto == "WTO"
replace tr20713200 = 13.7 if year == 1996 & wto == "WTO"
replace tr20714100 = 4.7  if year == 1996 & wto == "WTO"
replace tr20714210 = 9.8  if year == 1996 & wto == "WTO"
replace tr20714220 = 13.7 if year == 1996 & wto == "WTO"
* Applied MFN = WTO (96April-)
replace tr20711000 = 13.3 if ayear == 1996 & wto == "WTO"
replace tr20712000 = 13.3 if ayear == 1996 & wto == "WTO"
replace tr20713100 = 9.5  if ayear == 1996 & wto == "WTO"
replace tr20713200 = 13.3 if ayear == 1996 & wto == "WTO"
replace tr20714100 = 4.3  if ayear == 1996 & wto == "WTO"
replace tr20714210 = 9.5  if ayear == 1996 & wto == "WTO"
replace tr20714220 = 13.3 if ayear == 1996 & wto == "WTO"
* Applied MFN = WTO (1997)
replace tr20711000 = 13   if ayear == 1997 & wto == "WTO"
replace tr20712000 = 13   if ayear == 1997 & wto == "WTO"
replace tr20713100 = 9.3  if ayear == 1997 & wto == "WTO"
replace tr20713200 = 13   if ayear == 1997 & wto == "WTO"
replace tr20714100 = 4    if ayear == 1997 & wto == "WTO"
replace tr20714210 = 9.3  if ayear == 1997 & wto == "WTO"
replace tr20714220 = 13   if ayear == 1997 & wto == "WTO"
* Applied MFN = WTO (1998)
replace tr20711000 = 12.6 if ayear == 1998 & wto == "WTO"
replace tr20712000 = 12.6 if ayear == 1998 & wto == "WTO"
replace tr20713100 = 9    if ayear == 1998 & wto == "WTO"
replace tr20713200 = 12.6 if ayear == 1998 & wto == "WTO"
replace tr20714100 = 3.7  if ayear == 1998 & wto == "WTO"
replace tr20714210 = 9    if ayear == 1998 & wto == "WTO"
replace tr20714220 = 12.6 if ayear == 1998 & wto == "WTO"
* Applied MFN = WTO (1999)
replace tr20711000 = 12.3 if ayear == 1999 & wto == "WTO"
replace tr20712000 = 12.3 if ayear == 1999 & wto == "WTO"
replace tr20713100 = 8.8  if ayear == 1999 & wto == "WTO"
replace tr20713200 = 12.3 if ayear == 1999 & wto == "WTO"
replace tr20714100 = 3.3  if ayear == 1999 & wto == "WTO"
replace tr20714210 = 8.8  if ayear == 1999 & wto == "WTO"
replace tr20714220 = 12.3 if ayear == 1999 & wto == "WTO"

* GSP tariff rates PENDING
local regime = "GSP"
replace tr20711000 = . if regime == "`regime'"
replace tr20712000 = . if regime == "`regime'"
replace tr20713100 = . if regime == "`regime'"
replace tr20713200 = . if regime == "`regime'"
replace tr20714100 = 0 if regime == "`regime'"
replace tr20714210 = . if regime == "`regime'"  
replace tr20714220 = . if regime == "`regime'"
* LDC tariff rates PENDING
local regime = "LDC"
replace tr20711000 = 0 if regime == "`regime'"
replace tr20712000 = 0 if regime == "`regime'"
replace tr20713100 = 0 if regime == "`regime'"
replace tr20713200 = 0 if regime == "`regime'"
replace tr20714100 = . if regime == "`regime'"
replace tr20714210 = 0 if regime == "`regime'"
replace tr20714220 = 0 if regime == "`regime'"

* ASEAN
local regime = "ASEAN"
foreach ay in 2020 2019 2018 2017 2016 2015 2014 2013 2012 2011 2010 2009 2008{
replace tr20711000 = 11.9 if ayear ==`ay' & regime == "`regime'"
replace tr20712000 = 11.9 if ayear ==`ay' & regime == "`regime'"
replace tr20713100 = 8.5  if ayear ==`ay' & regime == "`regime'"
replace tr20713200 = 11.9 if ayear ==`ay' & regime == "`regime'"
replace tr20714100 = 0    if ayear ==`ay' & regime == "`regime'"
replace tr20714210 = 8.5  if ayear ==`ay' & regime == "`regime'"
replace tr20714220 = 11.9 if ayear ==`ay' & regime == "`regime'"
}
* CPTPP
local regime = "CPTPP"
foreach ay in 2020 2019{
replace tr20711000 = cond(`ay'==2020,1,0)*5.9 + cond(`ay'==2019,1,0)*7.9 ///
	if ayear == `ay' & regime == "`regime'"
replace tr20712000 = cond(`ay'==2020,1,0)*8.6 + cond(`ay'==2019,1,0)*9.7 ///
	if ayear == `ay' & regime == "`regime'"
replace tr20713100 = cond(`ay'==2020,1,0)*6.1 + cond(`ay'==2019,1,0)*6.9 ///
	if ayear == `ay' & regime == "`regime'"
replace tr20713200 = cond(`ay'==2020,1,0)*8.6 + cond(`ay'==2019,1,0)*9.7 ///
	if ayear == `ay' & regime == "`regime'"
replace tr20714100 = cond(`ay'==2020,1,0)*0.0 + cond(`ay'==2019,1,0)*0.0 ///
	if ayear == `ay' & regime == "`regime'"
replace tr20714210 = cond(`ay'==2020,1,0)*6.1 + cond(`ay'==2019,1,0)*6.9 ///
	if ayear == `ay' & regime == "`regime'"
replace tr20714220 = cond(`ay'==2020,1,0)*5.9 + cond(`ay'==2019,1,0)*7.9 ///
	if ayear == `ay' & regime == "`regime'"
}
* EU
local regime = "EU"
foreach ay in 2020 2019{
replace tr20711000 = cond(`ay'==2020,1,0)*6.0 + cond(`ay'==2019,1,0)*7.9 ///
	if ayear == `ay' & regime == "`regime'"
replace tr20712000 = cond(`ay'==2020,1,0)*8.7 + cond(`ay'==2019,1,0)*9.7 ///
	if ayear == `ay' & regime == "`regime'"
replace tr20713100 = cond(`ay'==2020,1,0)*6.2 + cond(`ay'==2019,1,0)*7.0 ///
	if ayear == `ay' & regime == "`regime'"
replace tr20713200 = cond(`ay'==2020,1,0)*8.7 + cond(`ay'==2019,1,0)*9.7 ///
	if ayear == `ay' & regime == "`regime'"
replace tr20714100 = cond(`ay'==2020,1,0)*0.0 + cond(`ay'==2019,1,0)*0.0 ///
	if ayear == `ay' & regime == "`regime'"
replace tr20714210 = cond(`ay'==2020,1,0)*6.2 + cond(`ay'==2019,1,0)*7.0 ///
	if ayear == `ay' & regime == "`regime'"
replace tr20714220 = cond(`ay'==2020,1,0)*6.0 + cond(`ay'==2019,1,0)*7.9 ///
	if ayear == `ay' & regime == "`regime'"
}
* USA
local cname = "USA"
foreach ay in 2020{
replace tr20711000 = .   if ayear ==`ay' & cnames == "`cname'"
replace tr20712000 = 8.6 if ayear ==`ay' & cnames == "`cname'"
replace tr20713100 = .   if ayear ==`ay' & cnames == "`cname'"
replace tr20713200 = .   if ayear ==`ay' & cnames == "`cname'"
replace tr20714100 = .   if ayear ==`ay' & cnames == "`cname'" 
replace tr20714210 = 6.1 if ayear ==`ay' & cnames == "`cname'" 
replace tr20714220 = 5.9 if ayear ==`ay' & cnames == "`cname'" 
}
* Singapore Malaysia Indonesia Brunei Switzerland Viet-Nam India Mongolia
foreach ay in 2020 2019 2018 2017 2016 2015 2014 2013 2012 2011 2010 2009 2008 2007 2006{
replace tr20714100 = 0    if ayear == `ay' & cnames == "Singapore" & ayear >= 2008
replace tr20714100 = 0    if ayear == `ay' & cnames == "Malaysia" & ayear >= 2006
replace tr20714100 = 0    if ayear == `ay' & cnames == "Indonesia" & ayear >= 2008
replace tr20714100 = 0    if ayear == `ay' & cnames == "Brunei" & ayear >= 2008
replace tr20714100 = 0    if ayear == `ay' & cnames == "Switzerland" & ayear >= 2010
replace tr20714100 = 0    if ayear == `ay' & cnames == "Viet Nam" & ayear >= 2010
replace tr20714100 = 0    if ayear == `ay' & cnames == "India" & ayear >= 2011
replace tr20714100 = 0    if ayear == `ay' & cnames == "Mongolia" & ayear >= 2017
}
* Thailand 
local cname = "Thailand"
foreach ay in 2020 2019 2018 2017 2016 2015 2014 2013 2012 2011 2010 2009 2008 2007{
local rate =  cond(`ay'==2020,1,0)*8.5  ///
            + cond(`ay'==2019,1,0)*8.5 ///
			+ cond(`ay'==2018,1,0)*8.5 ///
            + cond(`ay'==2017,1,0)*8.5 ///
			+ cond(`ay'==2016,1,0)*8.5 ///
            + cond(`ay'==2015,1,0)*8.5 ///
            + cond(`ay'==2014,1,0)*8.5 ///
            + cond(`ay'==2013,1,0)*8.5 ///
            + cond(`ay'==2012,1,0)*8.5 ///
            + cond(`ay'==2011,1,0)*9.1 ///
            + cond(`ay'==2010,1,0)*9.6 ///
            + cond(`ay'==2009,1,0)*10.2 ///
			+ cond(`ay'==2008,1,0)*10.8 ///
			+ cond(`ay'==2007,1,0)*11.3
replace tr20711000 = .      if ayear == `ay' & cnames == "`cname'"
replace tr20712000 = `rate' if ayear == `ay' & cnames == "`cname'"
replace tr20713100 = .      if ayear == `ay' & cnames == "`cname'"
replace tr20713200 = `rate' if ayear == `ay' & cnames == "`cname'"
replace tr20714100 = 0      if ayear == `ay' & cnames == "`cname'"
replace tr20714210 = .      if ayear == `ay' & cnames == "`cname'"
replace tr20714220 = `rate' if ayear == `ay' & cnames == "`cname'"
}

* Quotas Australia *************************************************************   
* meat: 20711* 20712* 20713* 207142* 16023121* 1602322* 16023921*
local cname = "Australia"
egen qm = rowtotal(kgrm20711* kgrm20712* kgrm20713* kgrm207142* ///
                   kgrm16023121* kgrm1602322* kgrm16023921*)
gen tonqm = qm/1000
bysort ayear cnum (amonth) : gen atonq = sum(tonqm)
foreach ay in 2020 2019 2018 2017 2016 2015 2014{
local quota = cond(`ay'==2020,1,0)*136 ///
            + cond(`ay'==2019,1,0)*120 ///
            + cond(`ay'==2018,1,0)*104 ///
            + cond(`ay'==2017,1,0)*88  ///
            + cond(`ay'==2016,1,0)*72  ///
            + cond(`ay'==2015,1,0)*56  ///
            + cond(`ay'==2014,1,0)*7
replace tr20711000 = 10.7 if ayear == `ay' & cnames == "`cname'" & atonq <= `quota'
replace tr20712000 = 10.7 if ayear == `ay' & cnames == "`cname'" & atonq <= `quota'
replace tr20713100 = 7.6  if ayear == `ay' & cnames == "`cname'" & atonq <= `quota'
replace tr20713200 = 10.7 if ayear == `ay' & cnames == "`cname'" & atonq <= `quota'
replace tr20714100 = 0    if ayear == `ay' & cnames == "`cname'" //& atonq <= `quota'
replace tr20714210 = 6.8  if ayear == `ay' & cnames == "`cname'" & atonq <= `quota'
replace tr20714220 = 8.5  if ayear == `ay' & cnames == "`cname'" & atonq <= `quota'
}
drop qm tonqm atonq

* Quotas PERU *************************************************************   
* meat: 20711* 20712* 207131* 2071421* 2071422* 16023121* 1602322* 16023921*
local cname = "Peru"
egen qm = rowtotal(kgrm20711* kgrm20712* kgrm207131* kgrm2071421* kgrm2071422* ///
                   kgrm16023121* kgrm1602322* kgrm16023921*)
gen tonqm = qm/1000
bysort ayear cnum (amonth) : gen atonq = sum(tonqm)
* Replace meat tariff rates
foreach ay in 2020 2019 2018 2017 2016 2015 2014 2013 2012 2011{
local quota = cond(`ay'==2020,1,0)*5500  ///
            + cond(`ay'==2019,1,0)*5500 ///
			+ cond(`ay'==2018,1,0)*5500 ///
            + cond(`ay'==2017,1,0)*5500 ///
			+ cond(`ay'==2016,1,0)*5500 ///
            + cond(`ay'==2015,1,0)*5500 ///
            + cond(`ay'==2014,1,0)*5000 ///
            + cond(`ay'==2013,1,0)*4500 ///
            + cond(`ay'==2012,1,0)*4000 ///
			+ cond(`ay'==2011,1,0)*292
replace tr20711000 = 10.7 if ayear == `ay' & cnames == "`cname'" & atonq <= `quota'
replace tr20712000 = 10.7 if ayear == `ay' & cnames == "`cname'" & atonq <= `quota'
replace tr20713100 = 7.6  if ayear == `ay' & cnames == "`cname'" & atonq <= `quota'
replace tr20713200 = .    if ayear == `ay' & cnames == "`cname'" //& atonq <= `quota'
replace tr20714100 = 0    if ayear == `ay' & cnames == "`cname'" //& atonq <= `quota'
replace tr20714210 = 6.8  if ayear == `ay' & cnames == "`cname'" & atonq <= `quota'
replace tr20714220 = cond(`ay'>=2013,1,0)*8.5 + cond(`ay'<=2012,0,1)*10.7 ///
if ayear == `ay' & cnames == "`cname'" & atonq <= `quota'
}
drop qm tonqm atonq

* Quotas Philippines ***********************************************************   
* meat: 20711* 20712* 207132* 2071422*
local cname = "Philippines"
egen qm = rowtotal(kgrm20711* kgrm20712* kgrm207132* kgrm2071422*)
gen tonqm = qm/1000
bysort ayear cnum (amonth) : gen atonq = sum(tonqm)
* Replace meat tariff rates
foreach ay in 2020 2019 2018 2017 2016 2015 2014 2013 2012 2011 2010 2009 2008{
local quota = cond(`ay'==2020,1,0)*7000  ///
            + cond(`ay'==2019,1,0)*7000 ///
			+ cond(`ay'==2018,1,0)*7000 ///
            + cond(`ay'==2017,1,0)*7000 ///
			+ cond(`ay'==2016,1,0)*7000 ///
            + cond(`ay'==2015,1,0)*7000 ///
            + cond(`ay'==2014,1,0)*7000 ///
            + cond(`ay'==2013,1,0)*7000 ///
            + cond(`ay'==2012,1,0)*7000 ///
            + cond(`ay'==2011,1,0)*6000 ///
            + cond(`ay'==2010,1,0)*5000 ///
            + cond(`ay'==2009,1,0)*4000 ///
			+ cond(`ay'==2008,1,0)*750
replace tr20711000 = 8.5 if ayear == `ay' & cnames == "`cname'" & atonq <= `quota'
replace tr20712000 = 8.5 if ayear == `ay' & cnames == "`cname'" & atonq <= `quota'
replace tr20713100 = .   if ayear == `ay' & cnames == "`cname'" //& atonq <= `quota'
replace tr20713200 = 8.5 if ayear == `ay' & cnames == "`cname'" & atonq <= `quota'
replace tr20714100 = 0   if ayear == `ay' & cnames == "`cname'" //& atonq <= `quota'
replace tr20714210 = .   if ayear == `ay' & cnames == "`cname'" //& atonq <= `quota'
replace tr20714220 = 8.5 if ayear == `ay' & cnames == "`cname'" & atonq <= `quota'
}
drop qm tonqm atonq

* Quotas Chile *************************************************************   
* meat: 2071422*
local cname = "Chile"
egen qm = rowtotal(kgrm2071422*)
gen tonqm = qm/1000
bysort ayear cnum (amonth) : gen atonq = sum(tonqm)
* Replace meat tariff rates
foreach ay in 2020 2019 2018 2017 2016 2015 2014 2013 2012 2011 2010 2009 2008 2007{
local quota = cond(`ay'==2020,1,0)*5500  ///
            + cond(`ay'==2019,1,0)*5500 ///
			+ cond(`ay'==2018,1,0)*5500 ///
            + cond(`ay'==2017,1,0)*5500 ///
			+ cond(`ay'==2016,1,0)*5500 ///
            + cond(`ay'==2015,1,0)*5500 ///
            + cond(`ay'==2014,1,0)*5500 ///
            + cond(`ay'==2013,1,0)*5500 ///
            + cond(`ay'==2012,1,0)*5500 ///
            + cond(`ay'==2011,1,0)*5500 ///
            + cond(`ay'==2010,1,0)*5000 ///
            + cond(`ay'==2009,1,0)*4500 ///
			+ cond(`ay'==2008,1,0)*4000 ///
			+ cond(`ay'==2007,1,0)*1750
replace tr20711000 = .   if ayear == `ay' & cnames == "`cname'" //& atonq <= `quota'
replace tr20712000 = .   if ayear == `ay' & cnames == "`cname'" //& atonq <= `quota'
replace tr20713100 = .   if ayear == `ay' & cnames == "`cname'" //& atonq <= `quota'
replace tr20713200 = .   if ayear == `ay' & cnames == "`cname'" //& atonq <= `quota'
replace tr20714100 = 0   if ayear == `ay' & cnames == "`cname'" //& atonq <= `quota'
replace tr20714210 = .   if ayear == `ay' & cnames == "`cname'" //& atonq <= `quota'
replace tr20714220 = 8.5 if ayear == `ay' & cnames == "`cname'" & atonq <= `quota'
}
drop qm tonqm atonq

* Quotas Mexico *************************************************************   
* meat: 20711* 20712* 20713* 207142* 16023121* 1602322* 16023921*
local cname = "Mexico"
egen qm = rowtotal(kgrm20711* kgrm20712* kgrm20713* kgrm207142* ///
                   kgrm16023121* kgrm1602322* kgrm16023921*)
gen tonqm = qm/1000
bysort ayear cnum (amonth) : gen atonq = sum(tonqm)
* Replace meat tariff rates
foreach ay in 2020 2019 2018 2017 2016 2015 2014 2013 2012 2011 2010 2009 2008 2007 2006{
local quota = cond(`ay'==2020,1,0)*9000  ///
            + cond(`ay'==2019,1,0)*9000 ///
			+ cond(`ay'==2018,1,0)*9000 ///
            + cond(`ay'==2017,1,0)*9000 ///
			+ cond(`ay'==2016,1,0)*9000 ///
            + cond(`ay'==2015,1,0)*8900 ///
            + cond(`ay'==2014,1,0)*8800 ///
            + cond(`ay'==2013,1,0)*8700 ///
            + cond(`ay'==2012,1,0)*8600 ///
            + cond(`ay'==2011,1,0)*8500 ///
            + cond(`ay'==2010,1,0)*8500 ///
            + cond(`ay'==2009,1,0)*8500 ///
			+ cond(`ay'==2008,1,0)*6000 ///
			+ cond(`ay'==2007,1,0)*4000 ///
			+ cond(`ay'==2006,1,0)*2500 
replace tr20711000 = cond(`ay'>=2012,1,0)*7.1 + cond(`ay'<=2011,0,1)*10.7 ///
	if ayear == `ay' & cnames == "`cname'" & atonq <= `quota'
replace tr20712000 = cond(`ay'>=2012,1,0)*7.1 + cond(`ay'<=2011,0,1)*10.7 /// 
	if ayear == `ay' & cnames == "`cname'" & atonq <= `quota'
replace tr20713100 = cond(`ay'>=2012,1,0)*5.1 + cond(`ay'<=2011,0,1)*7.6 ///
	if ayear == `ay' & cnames == "`cname'" & atonq <= `quota'
replace tr20713200 = cond(`ay'>=2012,1,0)*7.1 + cond(`ay'<=2011,0,1)*10.7 ///
	if ayear == `ay' & cnames == "`cname'" & atonq <= `quota'
replace tr20714100 = .  ///  
	if ayear == `ay' & cnames == "`cname'" //& atonq <= `quota'
replace tr20714210 = cond(`ay'>=2012,1,0)*5.1 + cond(`ay'<=2011 & `ay'>=2007,1,0)*6.8 + cond(`ay'<=2006,1,0)*7.6 ///
	if ayear == `ay' & cnames == "`cname'" & atonq <= `quota'
replace tr20714220 = cond(`ay'>=2012,1,0)*7.1 + cond(`ay'<=2011 & `ay'>=2007,1,0)*8.5 + cond(`ay'<=2006,1,0)*10.7 ///
	if ayear == `ay' & cnames == "`cname'" & atonq <= `quota'
}
drop qm tonqm atonq
********************************************************************************

gsort cnames year month
*** Drop others
foreach s of local others{
drop kgrm`s'
drop kjpy`s'
}
* Tariff Duty Revenue (each meat)
foreach s of local chicken{
gen tdrev`s' = kjpy`s'*(tr`s'/100) // tariff duty in kjpy
}
* Tariff Duty revenue (total meat)
egen tdrev_all = rowtotal(tdrev*) // 
	bysort cnum year (month) : gen ytdrev_all = sum(tdrev_all) // cumulate 12 months
* All meat weight imported
egen kgrm_all = rowtotal(kgrm*) // OK because we dorpped kgrm for others
	bysort cnum year (month) : gen ykgrm_all = sum(kgrm_all) // cumulate 12 months
* All meat value imported
egen kjpy_all = rowtotal(kjpy*) // OK because we dorpped kgrm for others
	bysort cnum year (month) : gen ykjpy_all = sum(kjpy_all) // cumulate 12 months
drop kgrm2* kjpy2*
drop tr2* tdrev2*

* Unit value
gen cif = kjpy_all/kgrm_all*1000 // JPY/kg
	gen ycif = ykjpy_all/ykgrm_all*1000 // JPY/kg
* Tariff duty
gen td = tdrev_all/kgrm_all*1000 // JPY/kg
	gen ytd = ytdrev_all/ykgrm_all*1000 // JPY/kg
* Price
gen pr = cif + td
	gen ypr = ycif + ytd
* Tariff rates
gen tr = td/cif
	gen ytr = ytd/ycif
* Sare
gen kjpy_dem = pr*kgrm_all/1000
egen csum = sum(kjpy_dem), by(time) // country-sum
*	egen ycsum = sum(ykjpy_all), by(time) // country-sum
gen lnsh = ln(kjpy_dem/csum)
*	gen lnysh = ln(ykjpy_dem/ycsum)
gen lnpr = ln(pr)
*	gen lnypr = ln(ypr)

*****************************************************************
tabulate time, generate(t)
tabulate cnum, generate(c)
xtset cnum time
* Instrumental Variables
gen lnjpylcc = ln(jpylcc)
bysort cnum year (month): gen yjpylcc = sum(jpylcc)
gen lnyjpylcc = ln(yjpylcc)


********************************************************************************
* Alternative test for endogeneity
gen lnce = ln(cif) - lnjpylcc
xtset cnum time
xtreg lnce lnjpylcc if ( ///
c53==1|c68==1|c31==1| c3==1| c8==1|c11==1|c26==1|c70==1|c36==1| ///
c67==1| c4==1| c1==1|c39==1|c42==1|c12==1|c79==1|c10==1|c81==1| ///
 c7==1|c46==1|c54==1|c52==1|c30==1 ///
), fe // NOT relevant => No feedback
xtreg lnce lnjpylcc if ( ///
c53==1|c68==1|c31==1| c3==1| c8==1|c11==1|c26==1|c70==1|c36==1| ///
c67==1| c4==1| c1==1|c39==1|c42==1|c12==1|c79==1|c10==1|c81==1| ///
 c7==1|c46==1|c54==1|c52==1|c30==1 ///
), fe vce(robust) // NOT relevant => No feedback
********************************************************************************

* MAIN REGRESSION * (and put the output into a .log file)

* FE(LS)
log using chicken_sigma_LS.log
xtreg lnsh t1-t300 lnpr if ( ///
c53==1|c68==1|c31==1| c3==1| c8==1|c11==1|c26==1|c70==1|c36==1| ///
c67==1| c4==1| c1==1|c39==1|c42==1|c12==1|c79==1|c10==1|c81==1| ///
 c7==1|c46==1|c54==1|c52==1|c30==1 ///
), fe vce(robust)
log close
* FE(IV)
log using chicken_sigma_IV.log
xtivreg2 lnsh t1-t300 (lnpr = jpylcc yjpylcc) if ( ///
c53==1|c68==1|c31==1| c3==1| c8==1|c11==1|c26==1|c70==1|c36==1| ///
c67==1| c4==1| c1==1|c39==1|c42==1|c12==1|c79==1|c10==1|c81==1| ///
 c7==1|c46==1|c54==1|c52==1|c30==1 ///
), fe robust bw(6) endog(lnpr)
log close
* DELTA METHOD
log using chicken_index.log
nlcom (sigma: 1-_b[lnpr])
forvalues i = 1/300{
nlcom (pindx: exp(-((0)+_b[t`i'])/_b[lnpr]))
}
log close
****************************************************
* MAC UNIX
* grep "pindx" chicken.log | grep "|" > chicken.txt
****************************************************

